﻿Public Class LightboxImage
	Inherits Control

	Shared Sub New()
		'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
		'This style is defined in Themes\Generic.xaml
		DefaultStyleKeyProperty.OverrideMetadata(GetType(LightboxImage), New FrameworkPropertyMetadata(GetType(LightboxImage)))
	End Sub

#Region " ImageSourceProperty Dependency Property "

#Region " ImageSource Property "

	Public Property ImageSource() As ImageSource
		Get
			Return DirectCast(GetValue(ImageSourceProperty), ImageSource)
		End Get
		Set(ByVal value As ImageSource)
			' Do not set the dependency property if there is something data bound to the property as this will distroy the property binding.
			Dim bindingExpression As BindingExpressionBase = TryCast(ReadLocalValue(LightboxImage.ImageSourceProperty), BindingExpressionBase)
			If bindingExpression Is Nothing Then SetValue(ImageSourceProperty, value)
		End Set
	End Property

#End Region

#Region " ImageSource Dependency Property "

	Public Shared ReadOnly ImageSourceProperty As DependencyProperty = DependencyProperty.Register("ImageSource", GetType(ImageSource), GetType(LightboxImage), New PropertyMetadata(Nothing, AddressOf OnImageSourcePropertyChanged))

#End Region

#Region " ImageSource Property Changed CallBack "

	Private Shared Sub OnImageSourcePropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		Dim source As LightboxImage = TryCast(d, LightboxImage)
		If source Is Nothing Then Throw New ArgumentException("source is not an instance of LightboxImage!")
		source.OnImageSourceChanged(d, e)
	End Sub

#End Region

#Region " ImageSource Overridable Property Changed "

	Protected Overridable Sub OnImageSourceChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		' Provide validation here. Developer may override this.
		RaiseImageSourceChangedEvent(d, e)
	End Sub

#End Region

#Region " Raise ImageSource Routed Event "

	Protected Overridable Sub RaiseImageSourceChangedEvent(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		' Do nothing except raise the event here.  Developer may override this and decide not to raise the event.
		RaiseEvent ImageSourceChanged(d, e)
	End Sub

#End Region

#Region " ImageSource Routed Event "

	Public Event ImageSourceChanged As PropertyChangedCallback

#End Region

#End Region

#Region " CaptionProperty Dependency Property "

#Region " Caption Property "

	Public Property Caption() As String
		Get
			Return DirectCast(GetValue(CaptionProperty), String)
		End Get
		Set(ByVal value As String)
			' Do not set the dependency property if there is something data bound to the property as this will distroy the property binding.
			Dim bindingExpression As BindingExpressionBase = TryCast(ReadLocalValue(LightboxImage.CaptionProperty), BindingExpressionBase)
			If bindingExpression Is Nothing Then SetValue(CaptionProperty, value)
		End Set
	End Property

#End Region

#Region " Caption Dependency Property "

	Public Shared ReadOnly CaptionProperty As DependencyProperty = DependencyProperty.Register("Caption", GetType(String), GetType(LightboxImage), New PropertyMetadata(String.Empty, AddressOf OnCaptionPropertyChanged))

#End Region

#Region " Caption Property Changed CallBack "

	Private Shared Sub OnCaptionPropertyChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		Dim source As LightboxImage = TryCast(d, LightboxImage)
		If source Is Nothing Then Throw New ArgumentException("source is not an instance of LightboxImage!")
		source.OnCaptionChanged(d, e)
	End Sub

#End Region

#Region " Caption Overridable Property Changed "

	Protected Overridable Sub OnCaptionChanged(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		' Provide validation here. Developer may override this.
		RaiseCaptionChangedEvent(d, e)
	End Sub

#End Region

#Region " Raise Caption Routed Event "

	Protected Overridable Sub RaiseCaptionChangedEvent(ByVal d As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
		' Do nothing except raise the event here.  Developer may override this and decide not to raise the event.
		RaiseEvent CaptionChanged(d, e)
	End Sub

#End Region

#Region " Caption Routed Event "

	Public Event CaptionChanged As PropertyChangedCallback

#End Region

#End Region

End Class
